// 字符串下划线转驼峰
export const formatToHump = function(value) {
  // eslint-disable-next-line
  return value.replace(/\_(\w)/g, (_, letter) => letter.toUpperCase())
}

// 字符串驼峰转下划线
export const formatToLine = function(value) {
  return value.replace(/([A-Z])/g, "_$1").toLowerCase()
}

// 是否是移动设备
export const isMobile = function(){
  return !!navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
}

// 分时函数
export const timeSlice = function(items, process, context, callback) {
  let index = 0
  const total = items.length

  function processBatch() {
    const startTime = performance.now()
    while (index < total && performance.now() - startTime < 16) { // 16ms 是一帧的时间
      process.call(context, items[index], index)
      index++
    }

    if (index < total) {
      requestAnimationFrame(processBatch)
    } else {
      callback && callback()
    }
  }

  requestAnimationFrame(processBatch)
}